import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note:子菜单只在路由子菜单时出现。长度> = 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                  如果设置为true，则item将不会显示在侧边栏中(默认为false)
 * alwaysShow: true              如果设置为true，将始终显示根菜单
 *                               如果没有设置alwaysShow，当item有多个子route时，
 *                               它将变成嵌套模式，否则不会显示根菜单
 * redirect: noRedirect          如果设置noRedirect将不会在面包屑中重定向
 * name:'router-name'           (必须设置!!)使用该名称。
 * meta : {
    roles: ['admin','editor']   控制页面角色(您可以设置多个角色)
    title: 'title'              名称显示在侧边栏和面包屑(推荐集)
    icon: 'svg-name'/'el-icon-x'图标显示在侧边栏
    breadcrumb: false           如果设置为false，该项目将隐藏在breadcrumb中(默认为true)
    activeMenu: '/example/list' 如果设置路径，工具条将突出显示您设置的路径
  }
 */

/**
 * constantRoutes
 * 没有权限要求的基本页
 * 可以访问所有角色
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [{
      path: 'home',
      name: 'home',
      component: () => import('@/views/home/index'),
      meta: { title: '首页', icon: 'home' }
    }]
  }
]

/**
 * asyncRoutes
 * 需要根据用户角色动态加载的路由
 */
export const asyncRoutes = [
  {
    path: '/systemConfig',
    component: Layout,
    redirect: '/systemConfig/users',
    name: 'systemConfig',
    meta: { title: '系统配置', icon: 'system', roles: ['admin', 'systemConfig'] },
    children: [
      {
        path: 'teams',
        name: 'teams',
        component: () => import('@/views/teams/index'),
        meta: { title: '部门管理', icon: 'teams', roles: ['admin', 'teams'] }
      },
      {
        path: 'roles',
        name: 'roles',
        component: () => import('@/views/roles/index'),
        meta: { title: '角色管理', icon: 'roles', roles: ['admin', 'roles'] }
      },
      {
        path: 'users',
        name: 'users',
        component: () => import('@/views/users/index'),
        meta: { title: '用户管理', icon: 'users', roles: ['admin', 'users'] }
      },
      {
        path: 'permissions',
        name: 'permissions',
        component: () => import('@/views/permissions/index'),
        meta: { title: '权限管理', icon: 'permissions', roles: ['admin', 'permissions'] }
      },
      {
        path: 'authorization',
        name: 'authorization',
        hidden: true,
        component: () => import('@/views/permissions/authorization'),
        meta: { title: '授权' }
      },
      {
        path: 'logs',
        name: 'logs',
        component: () => import('@/views/logs/index'),
        meta: { title: '日志管理', icon: 'logs', roles: ['admin', 'logs'] }
      }
    ]
  },
  {
    path: '/baseConfig',
    component: Layout,
    redirect: '/baseConfig/dataSource',
    name: 'baseConfig',
    meta: { title: '基础配置', icon: 'base', roles: ['admin', 'baseConfig'] },
    children: [
      {
        path: 'dataSource',
        name: 'dataSource',
        component: () => import('@/views/dataSource/index'),
        meta: { title: '数据源管理', icon: 'dataSource', roles: ['admin', 'dataSource'] }
      },
      {
        path: 'timedTask',
        name: 'timedTask',
        component: () => import('@/views/timedTask/index'),
        meta: { title: '定时任务管理', icon: 'timedTask', roles: ['admin', 'timedTask'] }
      }
    ]
  },
  {
    path: '/backup',
    component: Layout,
    redirect: '/backup/backupData',
    alwaysShow: true,
    meta: { title: '备份数据管理', icon: 'backupData', roles: ['admin', 'backupData'] },
    children: [{
      path: 'backupData',
      name: 'backupData',
      component: () => import('@/views/backupData/index'),
      meta: { title: '备份数据记录', icon: 'backupData', roles: ['admin', 'backupData'] }
    }]
  },
  {
    path: '/monitoring',
    component: Layout,
    redirect: '/monitoring/monTimedTask',
    alwaysShow: true,
    meta: { title: '监控管理', icon: 'monitoring', roles: ['admin', 'monitoring'] },
    children: [{
      path: 'monTimedTask',
      name: 'monTimedTask',
      component: () => import('@/views/monTimedTask/index'),
      meta: { title: '定时任务', icon: 'monTimedTask', roles: ['admin', 'monTimedTask'] }
    },
    {
      path: 'monitoringDetails',
      name: 'monitoringDetails',
      hidden: true,
      component: () => import('@/views/monTimedTask/monitoringDetails'),
      meta: { title: '监控详情' }
    }]
  },
  {
    path: '/msg',
    component: Layout,
    redirect: '/msg/msgList',
    alwaysShow: true,
    meta: { title: '消息管理', icon: 'msg', roles: ['admin', 'msg'] },
    children: [
      {
        path: 'msgList',
        name: 'msgList',
        component: () => import('@/views/msg/msgList'),
        meta: { title: '消息列表', icon: 'msgList', roles: ['admin', 'msgList'] }
      }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
